/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.momarketing.entity.CustomPage;
import com.boxin.ims.modules.momarketing.service.CustomPageService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;

/**
 * 自定义网页Controller
 * @author Jakemanse
 * @version 2014-03-21
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/momarketing/customPage")
public class CustomPageController extends BaseController {

	@Autowired
	private CustomPageService customPageService;
	
	@ModelAttribute
	public CustomPage get(@RequestParam(required=false) Long id) {
		if (id != null){
			return customPageService.get(id);
		}else{
			return new CustomPage();
		}
	}
	
	@RequiresPermissions("momarketing:customPage:view")
	@RequestMapping(value = {"list", ""})
	public String list(CustomPage customPage, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			customPage.setUser(user);
		}
        Page<CustomPage> page = customPageService.find(new Page<CustomPage>(request, response), customPage); 
        model.addAttribute("page", page);
		return "modules/momarketing/customPageList";
	}

	@RequiresPermissions("momarketing:customPage:view")
	@RequestMapping(value = "form")
	public String form(CustomPage customPage, Model model) {
	
		User user = UserUtils.getUser();
		if (customPage.getUser() == null || customPage.getUser().getId() <1){
			customPage.setUser(user);
		}
		
		model.addAttribute("customPage", customPage);
		return "modules/momarketing/customPageForm";
	}

	
	@RequiresPermissions("momarketing:customPage:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("picfile") MultipartFile picfile,CustomPage customPage, Model model,HttpServletRequest request, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, customPage)){
			return form(customPage, model);
		}
		
		if(picfile != null && picfile.getOriginalFilename().length()>4){
			String pageResourcePath = WechatUtils.getCustomPageResourceSavePath(request)+File.separator;
			FileUtils.createDirectory(pageResourcePath);
			String sname = picfile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_custom_"+System.currentTimeMillis()+fileType;
			
			if(StringUtils.isNotEmpty(customPage.getPicname())){
				fname = customPage.getPicname();
			}
			
			File file = new File(pageResourcePath+fname);
			System.out.println("保存文件:"+pageResourcePath+fname);
			
			try {
				picfile.transferTo(file);
				customPage.setPicname(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+pageResourcePath+fname);
				e.printStackTrace();
			}
			
		}
		customPage.setScore((int)(Double.parseDouble(request.getParameter("pScore"))*1000));
		customPageService.save(customPage);
		addMessage(redirectAttributes, "保存自定义网页'" + customPage.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/momarketing/customPage/?repage";
	}
	
	@RequiresPermissions("momarketing:customPage:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		customPageService.delete(id);
		addMessage(redirectAttributes, "删除自定义网页成功");
		return "redirect:"+Global.ADMIN_PATH+"/momarketing/customPage/?repage";
	}

}
